package com.zhumo.sz.gaode;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 高德道路对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Road {

    /**
     * 道路id
     */
    private String id;

    /**
     * 道路名称
     */
    private String name;

    /**
     * 定位点方向
     */
    private String direction;

    /**
     * 定位点距离
     */
    private Double distance;

    /**
     * 经度
     */
    private Double lng;

    /**
     * 纬度
     */
    private Double lat;

    public static Road build(JSONObject roadJson) {
        String location = roadJson.getString("location");
        Double lng = Double.valueOf(location.split(",")[0]);
        Double lat = Double.valueOf(location.split(",")[1]);
        return new Road(
                roadJson.getString("id"),
                roadJson.getString("name"),
                roadJson.getString("direction"),
                Double.valueOf(roadJson.getString("distance")),
                lng, lat
        );
    }

    /**
     * 转换格式为mysql中数据形式 便于后续etl
     */
    public static JSONObject parseRoadForMysql(String arrayStr) {
        try {
            JSONObject result = new JSONObject();
            JSONArray jsonArray = JSONArray.parseArray(arrayStr);
            jsonArray.forEach( json -> {
                JSONObject son = JSONObject.parseObject(JSON.toJSONString(json));
                JSONObject buildSon = (JSONObject) JSON.toJSON(Road.build(son));
                String roadId = buildSon.getString("id");
                result.put(roadId,buildSon);
            });
            return result;
        } catch (JSONException e) {
            e.printStackTrace();
            return new JSONObject();
        }
    }


}